# PiCalculator.rb
# June 29, 2007
#
require "bigdecimal"
require "bigdecimal/math"

include BigMath


def gauss_legendre
  p = 1002
  a = BigDecimal.new("1")
  b = BigDecimal.new("1")
  z = BigDecimal.new("2")
  b = b.div(z.sqrt(p), p)
  x = BigDecimal.new("1")
  t = BigDecimal.new("0.25")
  
  10.times do
    y = a 
    a = a.add(b, p).div(2,p)
    b = (b.mult(y, p)).sqrt(p)
    t = t.sub(x.mult((y.sub(a,p))**2, p),p)
    x = x.mult(2,p)
  end
  
  r = ((a.add(b, p))**2).div(t.mult(4,p),p)
  r
end

x = gauss_legendre
puts x.to_s('F')
